/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.seckill.platform.task;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.yami.shop.seckill.comment.model.Seckill;
import com.yami.shop.seckill.comment.service.SeckillOrderService;
import com.yami.shop.seckill.comment.service.SeckillService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


/**
 * 秒杀订单的定时任务
 * @author LGH
 */
@Component("seckillOrderTask")
@AllArgsConstructor
public class SeckillOrderTask {

    private final SeckillOrderService seckillOrderService;
    private final SeckillService seckillService;

    /**
     * 取消订单
     */
    public void cancelOrder(){
        seckillOrderService.cancelOrderUnpayOrderByTime();
    }

    /**
     * 秒杀结束，改变商品类型
     */
    public void seckillFinish(){
        List<Seckill> seckillList = seckillService.list(new LambdaQueryWrapper<Seckill>()
                .lt(Seckill::getEndTime, new Date())
                .ne(Seckill::getStatus, 0)
                .ne(Seckill::getIsDelete, 1)
        );
        if (seckillList.size() > 0){
            List<Long> seckillIds = Lists.newArrayList();
            for (Seckill seckill: seckillList){
                seckillIds.add(seckill.getSeckillId());
            }
            if (CollUtil.isNotEmpty(seckillIds)){
                seckillService.changeProdTypeBySeckillIdList(seckillIds);
            }
        }
    }
}
